//
//  WDGrabOrdersAddressTextView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/7.
//

#import "WDGrabOrdersAddressTextView.h"

@interface WDGrabOrdersAddressTextView () <UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *valueTxtField;
@property (nonatomic, copy) NSString *value;
@property (nonatomic, strong) CALayer *bottomLine;

@property (nonatomic, strong) UIImageView *locImgView;

@end

@implementation WDGrabOrdersAddressTextView

- (void)initWithSubView
{
    self.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    CGRect frame = CGRectMake(kRealValue(20), self.height - 1, self.width - kRealValue(20) * 2, 1);
    self.bottomLine = [CALayer drawLine:self frame:frame lineColor:[UIColor colorWithHexString:@"#EEEEEE"]];
    [self initWithTitleLabel];
    
    [self initWithValueTxtField];
    
    [self initWithLocImgView];
}

- (void)initWithTitleLabel
{
    if (!_titleLabel) {
        NSString *str = @"交付中心大家";
        float width = [str widthForFont:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium]];
        CGRect frame = CGRectMake(kRealValue(20), (self.height - 30) * 0.5, width, 30);
        _titleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
//            label.backgroundColor = [UIColor orangeColor];
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
}



- (void)initWithValueTxtField
{
    if (!_valueTxtField) {
        UIImage *image = [UIImage imageNamed:@"ic_loc_blue_small"];
        CGRect frame = CGRectMake(self.titleLabel.right, (self.height - 30) * 0.5, self.width - self.titleLabel.right - 20 - image.size.width - 5, 30);
        _valueTxtField = [[UITextField alloc] initWithFrame:frame];
        _valueTxtField.placeholder = @"请输入您的姓名";
        _valueTxtField.textAlignment = NSTextAlignmentRight;
        _valueTxtField.textColor = HexColor(@"#333333");
        _valueTxtField.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        _valueTxtField.borderStyle = UITextBorderStyleNone;
        _valueTxtField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _valueTxtField.delegate = self;
        [self addSubview:_valueTxtField];
    }
}

- (void)initWithLocImgView {
    if (!_locImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_loc_blue_small"];
        CGRect frame = CGRectMake(self.valueTxtField.right + 5, 0, image.size.width, image.size.height);
        _locImgView = [UIImageView createWithImageView:self frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            
        }];
    }
    _locImgView.centerY = self.valueTxtField.centerY;
}

- (void)setEnabled:(BOOL)enabled
{
    _valueTxtField.enabled = enabled;
}

- (void)setTitleFont:(UIFont *)titleFont
{
    _titleLabel.font = titleFont;
}

- (void)setTitleColor:(UIColor *)titleColor
{
    _titleLabel.textColor = titleColor;
}

- (void)setValueFont:(UIFont *)valueFont
{
    _valueTxtField.font = valueFont;
}

- (void)setValueColor:(UIColor *)valueColor
{
    _valueTxtField.textColor = valueColor;
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _valueTxtField.placeholder = placeholder;
}

- (void)setAttributedPlaceholder:(NSAttributedString *)attributedPlaceholder
{
    _valueTxtField.attributedPlaceholder = attributedPlaceholder;
}

- (void)setHiddenBottomLine:(BOOL)hiddenBottomLine
{
    _hiddenBottomLine = hiddenBottomLine;
    _bottomLine.hidden = hiddenBottomLine;
}



- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
    _valueTxtField.textAlignment = textAlignment;
}

- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titleLabel.text = title;
}

- (void)setIsRequired:(BOOL)isRequired {
    _isRequired = isRequired;
    if (isRequired) {
        self.titleLabel.attributedText = [EDSUtility attributeTitle:@"* " titleColor:[UIColor colorWithHexString:@"#F24C55"] titleFont:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] value:self.title valueColor:[UIColor colorWithHexString:@"#333333"] valueFont:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular]];
    } else {
        self.titleLabel.text = self.title;
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (self.touchEndEditBlock) {
        self.touchEndEditBlock();
    }
}

@end
